# =============================================================================
# April 24 2025
# R code used to produce all results, tables, and figures in the SI Online Appendix H
# Rebecca Cordell
# Unpacking the Role of In-Group Bias in US Public Opinion on Human Rights Violations
# American Journal of Political Science
# https://doi.org/10.7910/DVN/TGAL7M
# =============================================================================

# Clear work environment
rm(list=ls())

# Install Packages
#install.packages("dplyr")
#install.packages("ggplot2")
#install.packages("forcats")
#install.packages("ggpubr")
#install.packages("ggeasy")
#install.packages("lemon")
#install.packages("sandwich")
#install.packages("survey")
#install.packages("lmtest")
#install.packages("remotes")
#remotes::install_version("cregg", version = "0.4.0")
#install.packages("cjoint")

# Required Packages
library("dplyr")
library("ggplot2")
library("forcats")
library("ggpubr")
library("sandwich")
library("survey")
library("lmtest")
library("cregg")
library("ggeasy")
library("lemon")
library("cjoint")

options(scipen = 999)
options(warn=-1)

# -----------------------------------------------------------------------------
# Read in data
# -----------------------------------------------------------------------------

hr_survey<-read.csv("cordell_ingroupbiashumanrights_data.csv", header=TRUE, stringsAsFactors = FALSE)

# =============================================================================
# Figure H.1: Effect of Group Identity Attributes, Average Marginal Component Effects (AMCEs)
# =============================================================================

# Convert regression variables into factors
factor_vars <- c("perp_match", "agent", "type", "scope", "targ_nonstate",
                 "targ_race_match", "targ_relig_match", "targ_citiz_match",
                 "frame", "elite_match")
hr_survey[factor_vars] <- lapply(hr_survey[factor_vars], as.factor)

# Set baselines for attributes
baselines <- list(
  perp_match = "perp_in",
  agent = "a civilian militia group",
  type = "arbitrary arrest",
  scope = "two",
  targ_nonstate = "suspected terrorists",
  targ_race_match = "race_in",
  targ_relig_match = "relig_in",
  targ_citiz_match = "citiz_in",
  frame = "none",
  elite_match = "elite_in"
)

# Recode NA values for group identity attributes
levels(hr_survey$perp_match) <- c(levels(hr_survey$perp_match), "exclude")
hr_survey$perp_match[is.na(hr_survey$perp_match)] <- "exclude"
levels(hr_survey$targ_relig_match) <- c(levels(hr_survey$targ_relig_match), "exclude")
hr_survey$targ_relig_match[is.na(hr_survey$targ_relig_match)] <- "exclude"
levels(hr_survey$targ_race_match) <- c(levels(hr_survey$targ_race_match), "exclude")
hr_survey$targ_race_match[is.na(hr_survey$targ_race_match)] <- "exclude"
levels(hr_survey$elite_match) <- c(levels(hr_survey$elite_match), "exclude")
hr_survey$elite_match[is.na(hr_survey$elite_match)] <- "exclude"

# -----------------------------------------------------------------------------
# Calculate AMCEs
# -----------------------------------------------------------------------------

# Model 1
fig_h1_m1 <- summary(cjoint::amce(outcome1 ~ perp_match + agent + type + scope + targ_nonstate + targ_race_match + targ_relig_match + targ_citiz_match + frame + elite_match, data = hr_survey,
                                  cluster = TRUE, respondent.id = "id", baselines = baselines))$amce

# Subset group identity dummy variables
group_vars <- c("perp_match", "targ_race_match", 
                "targ_relig_match", "targ_citiz_match", 
                "elite_match")
fig_h1_m1 <- fig_h1_m1[fig_h1_m1$Attribute %in% group_vars & fig_h1_m1$Level != "exclude", ]

# Model 2
fig_h1_m2 <- summary(cjoint::amce(outcome2 ~ perp_match + agent + type + scope + targ_nonstate + targ_race_match + targ_relig_match + targ_citiz_match + frame + elite_match, data = hr_survey,
                                  cluster = TRUE, respondent.id = "id", baselines = baselines))$amce

# Subset group identity dummy variables
fig_h1_m2 <- fig_h1_m2[fig_h1_m2$Attribute %in% group_vars & fig_h1_m2$Level != "exclude", ]

# Create 95% confidence intervals for upper and lower bounds of estimates
critval <- 1.96 
fig_h1_m2$upper <- fig_h1_m2$Estimate + (critval * fig_h1_m2$`Std. Err`)
fig_h1_m2$lower <- fig_h1_m2$Estimate - (critval * fig_h1_m2$`Std. Err`)
fig_h1_m1$upper <- fig_h1_m1$Estimate + (critval * fig_h1_m1$`Std. Err`)
fig_h1_m1$lower <- fig_h1_m1$Estimate - (critval * fig_h1_m1$`Std. Err`)

# -----------------------------------------------------------------------------
# Prepare figures
# -----------------------------------------------------------------------------

# Combine models
fig_h1_m1$outcome<-"outcome1"
fig_h1_m2$outcome<-"outcome2"
fig_h1_all <- rbind(fig_h1_m1, fig_h1_m2)

# Add baseline values for in-groups
baseline_levels <- c("perp_in", "race_in", "relig_in", "citiz_in", "elite_in")
m1_in <- data.frame(
  Attribute = NA,
  Level = baseline_levels,
  Estimate = 0,
  `Std. Err` = NA,
  `z value` = NA,
  `Pr(>|z|)` = NA,
  " " = NA,
  upper = 0,
  lower = 0,
  outcome = "outcome1")
m2_in <- data.frame(
  Attribute = NA,
  Level = baseline_levels,
  Estimate = 0,
  `Std. Err` = NA,
  `z value` = NA,
  `Pr(>|z|)` = NA,
  " " = NA,
  upper = 0,
  lower = 0,
  outcome = "outcome2")
colnames(m1_in) <- colnames(fig_h1_all)
colnames(m2_in) <- colnames(fig_h1_all)
fig_h1_all <- rbind(fig_h1_all, m1_in, m2_in)

# Create group identity variable labels
variable_labels <- c(
  "perp_match" = "Perpetrator (partisanship)",
  "targ_race_match" = "Target (race)",
  "targ_relig_match" = "Target (religion)",
  "targ_citiz_match" = "Target (citizenship)",
  "elite_match" = "Elite cue (partisanship)"
)
fig_h1_all <- dplyr::bind_rows(
  fig_h1_all,
  do.call(rbind, lapply(names(variable_labels), function(variable) {
    data.frame(
      outcome = c("outcome1", "outcome2"),
      variable = variable_labels[variable],
      Level = variable_labels[variable],
      estimate = NA, lower = NA, upper = NA
    )
  }))
)

# Create respondents variable (in-groups vs. out-groups)
fig_h1_all <- fig_h1_all %>%
  mutate(
    Respondents = case_when(
      grepl("_in", Level) ~ "In-group",
      grepl("_out", Level) ~ "Out-group",
      Level %in% c("Perpetrator (partisanship)", "Target (race)", 
                   "Target (religion)", "Target (citizenship)", "Elite cue (partisanship)") ~ "In-group",
      TRUE ~ Level
    ),
    Respondents = factor(Respondents, levels = c("In-group", "Out-group"))
  )

# Set factor order
level_order <- c(
  "Perpetrator (partisanship)", "perp_in", "perp_out",
  "Target (race)", "race_in", "race_out",
  "Target (religion)", "relig_in", "relig_out",
  "Target (citizenship)", "citiz_in", "citiz_out",
  "Elite cue (partisanship)", "elite_in", "elite_out"
)
fig_h1_all <- fig_h1_all %>%
  mutate(Level = factor(Level, levels = level_order) %>% fct_rev())

# Separate models
fig_h1_m1 <- filter(fig_h1_all, outcome == "outcome1")
fig_h1_m2 <- filter(fig_h1_all, outcome == "outcome2")

# -----------------------------------------------------------------------------
# Create figures
# -----------------------------------------------------------------------------

# Create panel (a) Disapproval forced-choice
fig_h1_m1_plot <- ggplot(fig_h1_m1, aes(x = Level, y = Estimate, colour = Respondents)) +
  geom_point() +
  geom_errorbar(aes(ymin = lower, ymax = upper), size = 0.3, width = 0.2) +
  scale_x_discrete(labels = c(
    "", "Elite cue (partisanship)", "", 
    "", "Target (citizenship)", "", 
    "", "Target (religion)", "", 
    "", "Target (race)", "", 
    "","Perpetrator (partisanship)",""
  )) +
  xlab('') + ylab('Change: Pr(Disapproval of Violation)') +
  coord_flip() +
  geom_hline(yintercept = 0, size = 0.2, color = "black") +
  scale_y_symmetric(mid = 0) +
  theme_classic(base_size = 10) +
  scale_colour_grey() +
  easy_center_title() +
  ggtitle("(a) Disapproval forced-choice") +
  theme(
    axis.text.x = element_text(colour = "black"),
    axis.text.y = element_text(colour = "black"),
    axis.title.x = element_text(margin = margin(t = 10)),
    axis.ticks.y = element_blank()
  ) +
  labs(color = NULL)

# Create panel (b) Disapproval ratings-based
fig_h1_m2_plot <- ggplot(fig_h1_m2, aes(x = Level, y = Estimate, colour = Respondents)) +
  geom_point() +
  geom_errorbar(aes(ymin = lower, ymax = upper), size = 0.3, width = 0.2) +
  scale_x_discrete(labels = c(
    "", "Elite cue (partisanship)", "", 
    "", "Target (citizenship)", "", 
    "", "Target (religion)", "", 
    "", "Target (race)", "", 
    "","Perpetrator (partisanship)",""
  )) +
  xlab('') + ylab('Change: Disapproval of Violation (0-1)') +
  coord_flip() +
  geom_hline(yintercept = 0, size = 0.2, color = "black") +
  scale_y_symmetric(mid = 0) +
  theme_classic(base_size = 10) +
  scale_colour_grey() +
  easy_center_title() +
  ggtitle("(b) Disapproval ratings-based") +
  theme(
    axis.text.x = element_text(colour = "black"),
    axis.text.y = element_text(colour = "black"),
    axis.title.x = element_text(margin = margin(t = 10)),
    axis.ticks.y = element_blank()
  ) +
  labs(color = NULL)

# Print Figure H.1
pdf('figure_h1.pdf', width = 8.26, height = 5.82)
ggarrange(fig_h1_m1_plot, NULL, fig_h1_m2_plot,
          nrow = 1, common.legend = T, legend = "bottom", ncol=3, widths = c(1, 0.09, 1))
dev.off()